<!-- ajax的作用--在不刷新页面的情况下，向服务器发送数据，并接受服务器返回的数据 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script>
    window.onload = function() {
        var btn = document.getElementById('btn');

        btn.onclick = function() {
            var username = document.getElementById('username').value;
            //发送ajax请求
            //1.创建XMLHttpRequest对象
            var xhr = new XMLHttpRequest();
            //2.准备发送
            xhr.open('GET', "./ajax.php?username=" + username);
            //3.执行发送
            // xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.send(null);
            //4.执行回调函数 
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        // console.log(xhr.responseText);
                        // alert(xhr.responseText);
                        // console.log(xhr.responseText);
                        document.getElementById('gg').innerHTML = xhr.responseText;
                    }
                }
            }
        }
    }
</script>

<body>
    <form action="">
        用户名：<input type="text" name="username" id="username">
        <input type="button" name="" value="注册" id="btn">
    </form>
    <div class="gg" id="gg"></div>
</body>


</html>